package net.zjitc.dao.UserDaoImpl;

import net.zjitc.dao.UserDao;
import net.zjitc.daomain.User;
import net.zjitc.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * Created by hasee on 2017/9/27.
 */
public class UserDaoImpl implements UserDao{
    @Override
    public User findByUsername(String username) throws SQLException {
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSoure());
        String sql  = "select * from user where username = ?";
        User user = (User) runner.query(sql,new BeanHandler(User.class),username);
        return user;
    }
    @Override
    public boolean insert(User user){
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSoure());
        String sql = "insert into user(username,password) values(?,?)";
        try {
            runner.update(sql,user.getUsername(),user.getPassword());
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
